package cn.com.yto56.coresystem.common.msg.util.dwr;

import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangrui on 2017/2/23.
 */
public class DWRScriptSessionListener implements ScriptSessionListener {
    //维护一个Map key为session的Id， value为ScriptSession对象
    public static final Map<String, ScriptSession> scriptSessionMap = new HashMap<String, ScriptSession>();

    /**
     * ScriptSession创建事件
     */
    @Override
    public void sessionCreated(ScriptSessionEvent event) {
        WebContext webContext = WebContextFactory.get();
        if (webContext != null) {
            HttpSession session = webContext.getSession();
            ScriptSession scriptSession = event.getSession();
            scriptSessionMap.put(session.getId(), scriptSession);     //添加scriptSession
        }

    }

    /**
     * ScriptSession销毁事件
     */
    @Override
    public void sessionDestroyed(ScriptSessionEvent event) {
        WebContext webContext = WebContextFactory.get();
        if (webContext != null) {
            HttpSession session = webContext.getSession();
            scriptSessionMap.remove(session.getId());  //移除scriptSession
        }
    }

    /**
     * 获取所有ScriptSession
     */
    public static Collection<ScriptSession> getScriptSessions() {
        return scriptSessionMap.values();
    }
}
